package com.qll123.android.aop;

import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.qll123.android.R;
import com.qll123.module_aop.async.Async;
import com.qll123.module_aop.cache.Cacheable;
import com.qll123.module_aop.hook.AHook;
import com.qll123.module_aop.log.ALog;
import com.qll123.module_aop.permission.CheckPermission;
import com.qll123.module_aop.tracking.TrackEvent;
import com.qll123.module_aop.tracking.TrackParameter;


public class AopActivity extends AppCompatActivity implements View.OnClickListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_aop);
        findViewById(R.id.button_back).setOnClickListener(this);
        findViewById(R.id.button_track).setOnClickListener(this);
        findViewById(R.id.button_alog).setOnClickListener(this);
        findViewById(R.id.button_ahook).setOnClickListener(this);
        findViewById(R.id.button_async).setOnClickListener(this);
        findViewById(R.id.button_cache).setOnClickListener(this);
        findViewById(R.id.button_permisson).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button_back:{
                finish();
                break;
            }
            case R.id.button_track:{
                trackMethod(234,"trackmothod");
                break;
            }
            case R.id.button_alog:{
                alogMethod("alog");
                break;
            }
            case R.id.button_ahook:{
                hookMethod();
                break;
            }
            case R.id.button_cache:{
                cacheableMethod();
                break;
            }
            case R.id.button_permisson:{
                permissionMethod("permission");
                break;
            }
            case R.id.button_async:{
                asyncMethod();
                break;
            }
            default:
                break;
        }
    }


    @TrackEvent(eventName = "点击按钮", eventId = "100")
    private void trackMethod(
            @TrackParameter("uid") int uid,
            @TrackParameter("name") String name
    ) {
        int i= 100;
        i++;
        // 上报
        Log.e("MainActivity", "---->" + name+"---i---"+i);
    }


    @Async
    private void asyncMethod() {

        StringBuilder sb = new StringBuilder();
        sb.append("current thread=").append(Thread.currentThread().getId())
                .append("\r\n")
                .append("ui thread=")
                .append(Looper.getMainLooper().getThread().getId());


        Toast.makeText(AopActivity.this, sb.toString(), Toast.LENGTH_SHORT).show();
        Log.e("MainActivity", "---->" + sb.toString());

    }

    @ALog
    private String alogMethod(String s) {

        return s;
    }

    @Cacheable(key = "address")
    private Addresss cacheableMethod() {
        Addresss address = new Addresss();
        address.country = "China";
        address.province = "Jiangsu";
        address.city = "Suzhou";
        address.street = "Ren min Road";

        return address;
    }

    @CheckPermission("android.permission.CALL_PHONE")
    private String permissionMethod(String s) {

        return s;
    }



    @AHook(beforeMethod = "method1",afterMethod = "method2")
    private void hookMethod() {

        Log.e("MainActivity", "initData()");
    }

    private void method1() {
        Log.e("MainActivity", "method1() is called before initData()");
    }

    private void method2() {
        Log.e("MainActivity", "method2() is called after initData()");
    }
}
